package cc.fs.androidcode.utils.mqtt;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by fostion on 2015/7/16.
 * 推送服务运行在后台用作接收推送
 */
public class PushService extends Service {
    private static final String ACTION = "action";
    private static final int FLAG_START = 0;
    private static final int FLAG_STOP = 1;

    private MyBinder myBinder = new MyBinder();
    private MqttClient client;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, PushService.class);
        intent.putExtra(ACTION, FLAG_START);
        context.startService(intent);
    }

    public static void actionStop(Context context) {
        Intent intent = new Intent(context, PushService.class);
        intent.putExtra(ACTION, FLAG_STOP);
        context.startService(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            int action = intent.getIntExtra(ACTION, FLAG_START);
            switch (action) {
                case FLAG_START:
                    if (client == null) {
//                        userBean = AppContext.getMyUser();
//                        if(userBean != null){
//                            client = MqttClient.getInstance(userBean.getId());
//                            client.setResponse(new MqttClient.Response<SysMsgBean>() {
//                                @Override
//                                public void run(SysMsgBean sysMsg) {
//                                    if (sysMsg != null) {
//                                    Message msg = new Message();
//                                    msg.what = BaseActivity.FLAG_PUSH_MSG;
//                                    msg.obj = sysMsg;
//                                    BaseActivity.broadcast(msg);
//                                        MqttUtils.show(PushService.this, sysMsg);
//                                    }
//                                }
//                            });
//                        }
                    } else {
                        client.reSubcribe(new MqttClient.Response<Boolean>() {
                            @Override
                            public void run(Boolean param) {
                            }
                        });
                    }
                    break;

                case FLAG_STOP:
                    NotificationManager mNotifyMgr = (NotificationManager) PushService.this.getSystemService(Service.NOTIFICATION_SERVICE);
                    mNotifyMgr.cancelAll();
                    if(client != null){//remove subcribe
                        client.removeSubcribe();
                        client.close();
                        client = null;
                    }
                    break;

            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    class MyBinder extends Binder {
        public PushService getService() {
            return PushService.this;
        }
    }

    @Override
    public void onDestroy() {
        if (client != null) {
            client.close();
        }
        super.onDestroy();
    }
}
